<?php

namespace App\Cache;

use App\Helpers\DateUtil;
use App\Helpers\RedisUtil;

class InviteVideoCache
{
    public static function buildKey($array){
        $script = <<<lua
            local key = redis.call('exists',KEYS[1])
            if key == 1 then
                return 0
            else
                redis.call('sAdd',KEYS[1],0)
                redis.call('Expire',KEYS[1],ARGV[1])
            end
        lua;

        $redis = RedisUtil::getRedis();

        foreach ($array as $item) {
            $redis->eval($script,[
                $item,
                DateUtil::getTodayEnds() + 120
            ],1);
        }
    }

//    /**
//     * 删除今日配对逻辑
//     * @param $maleId
//     * @param $femaleId
//     * @return void
//     */
//    public static function delTodayJoinLock($maleId,$femaleId)
//    {
//        $todayJoinLockKey = RedisKey::todayJoinLock();
//
//        $redis = RedisUtil::getRedis();
//
//        $redis->sRem($todayJoinLockKey,$maleId. ':' .$femaleId);
//    }

    /**
     * 添加邀请列表
     * @param $maleId
     * @param $femaleId
     * @return void
     */
    public static function addInviteList($maleId,$femaleId){
        $maleKey = RedisKey::todayInviteVideoList($maleId);
        $femaleKey = RedisKey::todayInviteVideoList($femaleId);
        $todayJoinLockKey = RedisKey::todayJoinLock();
        $key = RedisKey::joinInviteVideoToday();

        $redis = RedisUtil::getRedis();

        $script = <<<lua
            local male = redis.call('exists',KEYS[1])
            if male == 1 then
                redis.call('sAdd',KEYS[1],ARGV[1])
            else
                redis.call('sAdd',KEYS[1],ARGV[1])
                redis.call('Expire',KEYS[1],ARGV[3])
            end

            local female = redis.call('exists',KEYS[2])
            if female == 1 then
                redis.call('sAdd',KEYS[2],ARGV[2])
            else
                redis.call('sAdd',KEYS[2],ARGV[2])
                redis.call('Expire',KEYS[2],ARGV[3])
            end
            
            local todayLock = redis.call('exists',KEYS[3])
            if todayLock == 1 then
                redis.call('sAdd',KEYS[3],ARGV[4])
            else
                redis.call('sAdd',KEYS[3],0)
                redis.call('sAdd',KEYS[3],ARGV[4])
            end
            
            local joinList = redis.call('exists',KEYS[4])
            if joinList == 1 then
                redis.call('sAdd',KEYS[4],ARGV[1])
                redis.call('sAdd',KEYS[4],ARGV[2])
            else
                redis.call('sAdd',KEYS[4],0)
                redis.call('sAdd',KEYS[4],ARGV[1])
                redis.call('sAdd',KEYS[4],ARGV[2])
                redis.call('Expire',KEYS[4],ARGV[3])
            end
        lua;

        $redis->eval($script,[
            $maleKey,
            $femaleKey,
            $todayJoinLockKey,
            $key,
            $femaleId,
            $maleId,
            DateUtil::getTodayEnds() + 120,
            $maleId.':'.$femaleId
        ],4);
    }

    /**
     * 今日配对锁定
     * @param $maleId
     * @param $femaleId
     * @return bool
     */
    public static function pairLock($maleId,$femaleId)
    {
        $todayJoinLockKey = RedisKey::todayJoinLock();

        $redis = RedisUtil::getRedis();

        if ($redis->sIsMember($todayJoinLockKey, $maleId . ':' . $femaleId) == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 删除指定用户集合
     * @param $userId
     * @param $userIdTwo
     * @return void
     */
    public static function delInviteRefuse($userId,$userIdTwo)
    {
        $Key = RedisKey::todayInviteVideoList($userId);
        $KeyTwo = RedisKey::todayInviteVideoList($userIdTwo);

        $redis = RedisUtil::getRedis();

        $redis->sRem($Key,$userIdTwo);
        $redis->sRem($KeyTwo,$userId);
    }

    public static function delUserInviteList($userId)
    {
        $Key = RedisKey::todayInviteVideoList($userId);
        $joinKey = RedisKey::joinInviteVideoToday();

        $redis = RedisUtil::getRedis();

        $redis->del($Key);
        $redis->sRem($joinKey,$userId);
    }

    /**
     * 加锁
     * @param $userId
     * @param $gender
     * @return mixed
     */
    public static function lockUserInviteVideo($userId, $gender){
        if ($gender == 0){
            $key = RedisKey::inviteVideoFemaleLock();
        }else{
            $key = RedisKey::inviteVideoMaleLock();
        }


        $redis = RedisUtil::getRedis();

        $script = <<<lua
            local lock = redis.call('exists',KEYS[1])
            if lock == 1 then
                redis.call('sAdd',KEYS[1],ARGV[1])
            else
                redis.call('sAdd',KEYS[1],0)
                redis.call('sAdd',KEYS[1],ARGV[1])
                redis.call('Expire',KEYS[1],ARGV[2])
            end
        lua;

        return $redis->eval($script,[
            $key,
            $userId,
            DateUtil::getTodayEnds() + 120
        ],1);
    }

    /**
     * 是否锁了
     * @param $userId
     * @param $gender
     * @return bool
     */
    public static function isLock($userId, $gender): bool
    {
        if ($gender == 0){
            $key = RedisKey::inviteVideoFemaleLock();
        }else{
            $key = RedisKey::inviteVideoMaleLock();
        }

        $redis = RedisUtil::getRedis();

        if ($redis->sIsMember($key,$userId)){
            return true;
        }

        return false;
    }

    /**
     * 解锁
     * @param $userId
     * @param $gender
     * @return void
     */
    public static function delLock($userId, $gender)
    {
        if ($gender == 0){
            $key = RedisKey::inviteVideoFemaleLock();
        }else{
            $key = RedisKey::inviteVideoMaleLock();
        }

        $redis = RedisUtil::getRedis();

        $redis->sRem($key,$userId);
    }
}